﻿/*

Copyright 2009 Guillaume Amringer

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

package {
	import lt.uza.utils.Global;
	import org.papervision3d.core.math.Number3D;
	
	public class UnitsManager {
		
		public var globals:Global = Global.getInstance();
		
		private var _id:uint = 0;
		private var units:Object = new Object();
		
		public var selection:UnitsHolder = new UnitsHolder();
		
		
		public function UnitsManager():void {
			
		}
		
		public function registerNewUnit(pUnit:Unit):void {
			pUnit.SEXid = ++_id;
			globals.game.map.addChild(pUnit);
			units[pUnit.SEXid] = pUnit;
			pUnit.owner.units.addUnit(pUnit.SEXid);
		}
		
		public function fireEnterFrameEvent():void {
			for each(var unit:Unit in units)
				unit.enterFrameEvent();
		}
		
		public function addToSelection(pId:uint):void {
			selection.addUnit(pId);
		}
		
		public function setSelection(pIds:Array):void {
			selection.clear();
			for each(var id:uint in pIds)
				selection.addUnit(id);
		}
		
		public function clearSelection():void {
			selection.clear();
		}
		
		public function moveSelection(pDestination:Number3D):void {
			for each(var id:uint in selection.unitIds) {
				var unit:Unit = units[id];
				unit.destination = pDestination;
				unit.accelerate();
			}
		}
	}
}